package com.david.warehouse.service.impl;

import com.david.common.utils.EntityUtils;
import com.david.warehouse.mapper.CategoryMapper;
import com.david.warehouse.mapper.ProductMapper;
import com.david.warehouse.pojo.entity.Category;
import com.david.warehouse.pojo.entity.Product;
import com.david.warehouse.pojo.entity.Type;
import com.david.warehouse.pojo.vo.CategoryCasVO;
import com.david.warehouse.service.CategoruyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoruyServiceImpl implements CategoruyService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public void addCategory(String name,Integer typeId, Integer parentId) {
        categoryMapper.insertCategory(name,typeId, parentId);
    }

    @Override
    public List<CategoryCasVO> getCategoryList(Integer typeId) {
        List<CategoryCasVO> categoryList = categoryMapper.getCategoryListByTypeId(typeId);
        List<CategoryCasVO> categories = EntityUtils.listToCascader(categoryList);
        System.out.println(categories);
        return categories;
    }

    @Override
    public Type getType(String type) {
        return categoryMapper.selectType(type);
    }

    @Override
    public void updateCategory(Integer id, String name) {
        categoryMapper.updateCategory(id, name);
    }

    @Override
    public String deleteCategory(Integer id) {
        List<Category> categories = categoryMapper.selectCategoryByParentId(id);
        if (!categories.isEmpty()) return "包含子类别，请先删除子类别，删除失败";

        List<Product> materials = productMapper.selectMaterialListByCategoryId(id);
        if(!materials.isEmpty()) return "包含物料，请先删除物料，删除失败";

        categoryMapper.deleteCategoryById(id);
        return "completed";


    }
}
